22 Lecture

CS201

Midterm & Final Term Short Notes

Bitwise Manipulation and Assignment Operator

Bitwise manipulation involves performing operations on the individual bits of binary representations of numbers. Bitwise AND, OR, XOR, and NOT operators are used to manipulate bits. These operators are commonly used in computer programming, espe


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the result of the following bitwise AND operation?

    yaml
    1010 & 1101

    a) 0000 b) 1000 c) 1010 d) 1101

    Answer: b) 1000

  2. What is the result of the following bitwise OR operation?

    yaml
    1010 | 1101

    a) 0000 b) 1000 c) 1011 d) 1111

    Answer: c) 1011

  3. What is the result of the following bitwise XOR operation?

    yaml
    1010 ^ 1101

    a) 0000 b) 0111 c) 1011 d) 1111

    Answer: c) 1011

  4. What is the result of the following left shift operation?

    bash
    1010 << 2

    a) 0010 b) 0100 c) 1000 d) 101000

    Answer: d) 101000

  5. What is the result of the following right shift operation?

    yaml
    1010 >> 2

    a) 0010 b) 0100 c) 1000 d) 0000

    Answer: a) 0010

  6. What is the result of the following unsigned right shift operation?

    diff
    -10 >>> 2

    a) 0010 b) 0100 c) 1000 d) 11111111111111111111111111110101

    Answer: d) 11111111111111111111111111110101

  7. What is the result of the following bitwise NOT operation?

    ~1010

    a) 0101 b) 0101 c) 1101 d) 01011

    Answer: c) 0101

  8. What is the result of the following bitwise AND assignment operation?

    css
    int a = 1010; a &= 1101;

    a) a = 0000 b) a = 1000 c) a = 1010 d) a = 1101

    Answer: b) a = 1000

  9. What is the result of the following bitwise XOR assignment operation?

    css
    int a = 1010; a ^= 1101;

    a) a = 0000 b) a = 0111 c) a = 1011 d) a = 1111

    Answer: c) a = 1011

  10. What is the result of the following left shift assignment operation?

    css
    int a = 1010; a <<= 2;

    a) a = 0010 b) a = 0100 c) a = 1000 d) a = 101000

    Answer: d) a = 101000



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is bitwise AND operator and how does it work? Answer: The bitwise AND operator is represented by the symbol "&". It operates on two binary numbers by performing an AND operation on each corresponding pair of bits. The result is a binary number with a 1 in each bit position where both corresponding bits were 1 in the input numbers. For example, 1101 & 1011 = 1001.

  2. What is the purpose of bitwise XOR operator? Answer: The bitwise XOR operator is represented by the symbol "^". It operates on two binary numbers by performing an XOR operation on each corresponding pair of bits. The result is a binary number with a 1 in each bit position where the corresponding bits are different in the input numbers. The purpose of this operator is to flip the bits in the output where the input bits differ, which can be useful for various purposes such as encryption.

  3. What is left shift operator and how does it work? Answer: The left shift operator is represented by the symbol "<<". It operates on a binary number by shifting all of its bits to the left by a specified number of positions. The result is a binary number with 0s shifted in on the right side. For example, 1010 << 2 = 101000.

  4. What is the difference between signed and unsigned right shift operator? Answer: The signed right shift operator ">>" preserves the sign of the input number when shifting its bits to the right. The unsigned right shift operator ">>>" fills in 0s on the left side when shifting its bits to the right, regardless of the sign of the input number.

  5. What is bitwise NOT operator and how does it work? Answer: The bitwise NOT operator is represented by the symbol "~". It operates on a binary number by flipping all of its bits from 0 to 1 and 1 to 0. The result is the one's complement of the input number. For example, ~1010 = 0101.

  6. What is the purpose of bitwise OR operator? Answer: The bitwise OR operator is represented by the symbol "|". It operates on two binary numbers by performing an OR operation on each corresponding pair of bits. The result is a binary number with a 1 in each bit position where at least one corresponding bit is 1 in the input numbers. The purpose of this operator is to combine sets of binary flags or to set particular bits in a binary number.

  7. How do you perform a bitwise AND assignment operation in C++? Answer: To perform a bitwise AND assignment operation in C++, the "&=" operator is used. For example, if a is a variable containing the binary number 1010 and b is a variable containing the binary number 1101, the statement "a &= b;" will perform a bitwise AND operation on a and b, and store the result in a.

  8. What is the purpose of bitwise assignment operators? Answer: The purpose of bitwise assignment operators is to combine the operations of bitwise operators and assignment operators. These operators are used to modify the value of a variable in place using a bitwise operation. They are a convenient and efficient way to modify individual bits of a variable.

  9. How do you perform a left shift assignment operation in Java? Answer: To perform a left shift assignment operation in Java, the "<<=" operator is used. For example, if a is a variable containing the binary number 1010, the statement "a <<= 2;" will shift all of the bits in a to the left by 2 positions, resulting in the binary number 101000.

  10. What is the difference between prefix and postfix increment operators in C++? Answer: The prefix increment operator "++

Bitwise manipulation involves performing operations on the individual bits of binary representations of numbers. Bitwise operators are used to manipulate bits. These operators are commonly used in computer programming, especially in low-level programming such as device drivers, embedded systems, and graphics programming. Bitwise operators perform operations on each individual bit of the operands. The bitwise AND operator is represented by the symbol "&". It operates on two binary numbers by performing an AND operation on each corresponding pair of bits. The result is a binary number with a 1 in each bit position where both corresponding bits were 1 in the input numbers. The bitwise OR operator is represented by the symbol "|". It operates on two binary numbers by performing an OR operation on each corresponding pair of bits. The result is a binary number with a 1 in each bit position where at least one corresponding bit is 1 in the input numbers. The bitwise XOR operator is represented by the symbol "^". It operates on two binary numbers by performing an XOR operation on each corresponding pair of bits. The result is a binary number with a 1 in each bit position where the corresponding bits are different in the input numbers. The bitwise NOT operator is represented by the symbol "~". It operates on a binary number by flipping all of its bits from 0 to 1 and 1 to 0. The result is the one's complement of the input number. In addition to the basic bitwise operators, there are also bitwise shift operators. The left shift operator is represented by the symbol "<<". It operates on a binary number by shifting all of its bits to the left by a specified number of positions. The result is a binary number with 0s shifted in on the right side. The right shift operator is represented by the symbol ">>". It operates on a binary number by shifting all of its bits to the right by a specified number of positions. The result is a binary number with 0s shifted in on the left side. Bitwise assignment operators combine bitwise operations with assignment. They are used to modify the value of a variable in place using a bitwise operation. These operators are a convenient and efficient way to modify individual bits of a variable. Some examples of bitwise assignment operators include "&=", "|=", "^=", "<<=", and ">>=". It is important to note that bitwise operators are typically used on integer types in programming languages. When working with floating-point numbers, bitwise operations can produce undefined behavior. Additionally, bitwise operators should be used with caution when working with signed integer types, as the behavior of the right shift operator can be implementation-dependent.